﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Monocle.DataProvider;
using Monocle.Models;

namespace Monocle.Controllers
{
    public class AppController : Controller
    {

        public ActionResult Edit(Guid id, IDataProvider dataProvider)
        {
            var dto = dataProvider.GetApps().FirstOrDefault(); //TODO: If multiple sites are allowed, we should show a list or something
            var tPageList = dataProvider.GetPagesByApp(dto.PK_tApp).OrderBy(x => x.AppOrder).ToList();
            var viewModel = new AppViewModel(dto)
            {
                PageList = new PageCollectionViewModel(tPageList)
            };
            return View(viewModel);
        }

        /// <summary>
        /// Accepts a SiteViewModel. Attempts to save it in the database
        /// </summary>
        /// <param name="dataProvider"></param>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(AppViewModel viewModel, IDataProvider dataProvider)
        {
            if (!ModelState.IsValid)
            {
                //The viewmodel is not valid. Return input and the view
                return View(viewModel);
            }
            else
            {
                //Get the old DTO from the database
                var dbDTO = dataProvider.GetApp(viewModel.ID);

                //Merge it with the new (update fields)
                UpdateModel(dbDTO);
                try
                {
                    //    Save the site to the database
                    viewModel.UpdateFromDTO(dataProvider.SaveApp(dbDTO));
                }
                catch (Exception e)
                {
                    //        If an error happens add that to the modelstate and return view
                    ModelState.AddModelError("", "En fejl er opstået: " + e.Message);
                    return View(viewModel);
                }

                //Update PageItem order if changed
                if (viewModel.PageOrder != null)
                {
                    var pageItemGuids = viewModel.PageOrder.Split(',').ToList();
                    var pageItemList = pageItemGuids.Select(item => dataProvider.GetPage(new Guid(item))).ToList();

                    var order = 1;
                    foreach (var item in pageItemList)
                    {
                        item.AppOrder = order;
                        try
                        {
                            dataProvider.SavePage(item);
                        }
                        catch (Exception e)
                        {
                            //        If an error happens add that to the modelstate and return view
                            ModelState.AddModelError("", "En fejl er opstået: " + e.Message);
                            return View(viewModel);
                        }
                        order++;
                    }
                }

                //Redirect the user to somewhere else. Could be a success page
                ViewBag.Status = Monocle.Resources.Monocle.Generic_Saved;

                viewModel.PageList = new PageCollectionViewModel(dataProvider.GetPagesByApp(viewModel.ID).OrderBy(x => x.AppOrder).ToList());
                return View(viewModel);
            }
        }
    }
}
